package message.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import message.annotation.MsgHandler;
import message.utils.JsonUtils;
import org.dom4j.*;
import message.utils.XmlUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MsgHandler(name = "json")
public class JsonMsghandler extends BaseHandler {

    @Override
    public void doPack(Map source) throws Exception {
        Map data = new HashMap();
        packDataToMap(this.msgConfig.getDocumentElement(), data, source);
        String s = JSON.toJSONString(data);
        this.msgData = s.getBytes(this.msgConfig.getEncoding());
    }

    @Override
    protected void doUnPack(byte[] rspByte) throws Exception {
        String json = new String(rspByte, this.msgConfig.getEncoding());
        JSONObject jsonObject = JSON.parseObject(json);
        Map<String, Object> source = JsonUtils.getJavaDict(jsonObject);
        Map result = new HashMap();
        unPackDataToMap(this.msgConfig.getDocumentElement(), result, source);
        this.unPackResult = result;
    }

    @Override
    public void packInit() {

    }

    @Override
    public void unPackInit() {

    }
}
